package com.msos.servlet.SMSQuery;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.msos.model.Message.Message;
import com.msos.model.RespPageBean.RespPageBean;
import com.msos.model.customer.Customer;
import com.msos.model.respBean.RespBean;
import com.msos.service.SMSQuery.SMSQueryService;
import com.msos.utils.CommonsUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @ClassName: SMSQueryServlet
 * @Description: TODO
 * @Author: hsx
 * @Date: 2022/4/13 9:34
 * @Version: v1.0
 */
@WebServlet(urlPatterns = "/smsquery")
public class SMSQueryServlet extends HttpServlet {
    SMSQueryService smsQueryService = new SMSQueryService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        //展示页面
        if ("page".equals(action)){
            req.getRequestDispatcher("/WEB-INF/jsp/SMSQuery/SMSQuery.jsp").forward(req,resp);
        }
        //获取该用户发送的所有信息(默认按照时间递减顺序)
        else if ("data".equals(action)){
            String mid = req.getParameter("mid");
            Customer customer = (Customer) req.getSession().getAttribute("loginCustomer");
            String page = req.getParameter("page");
            String rows = req.getParameter("rows");
            String sort = req.getParameter("sort");
            String order = req.getParameter("order");
            RespPageBean respPageBean = smsQueryService.getAllMessage(mid,customer,page,rows,sort,order);
            resp.setContentType("application/json;charset=utf-8");
            resp.getWriter().write(new ObjectMapper().writeValueAsString(respPageBean));
        }
        else if ("data_from_smsquery".equals(action)){
            Customer customer = (Customer) req.getSession().getAttribute("loginCustomer");
            List<Message> list = smsQueryService.getSendDateBySephone(customer);
            Message message = new Message();
            message.setMid(-1);
            message.setSendDate("全部");
            list.add(0,message);
            resp.setContentType("application/json;charset=utf-8");
            resp.getWriter().write(new ObjectMapper().writeValueAsString(list));
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    //根据信息id删除已发送的信息
    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String mid = req.getParameter("mid");
        Integer result = smsQueryService.deleteMessageByMid(Integer.parseInt(mid));
        RespBean respBean = null;
        if (result== CommonsUtils.EXCEPTION_ERROR){
            respBean = RespBean.error("删除失败！");
        }else {
            respBean = RespBean.ok("删除成功！");
        }
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(new ObjectMapper().writeValueAsString(respBean));
    }
}
